package com.speedricemanager.model.home;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import com.jbm.core.iface.IAcross;
import com.jbm.jsm.iface.AttachmentEntity;
import com.jbm.jsm.iface.OrderEntity;
import com.jbm.jsm.iface.SmallAccountEntity;
import com.jbm.jsm.iface.TaskEntity;
import com.speedricemanager.other.config.Config;
import com.speedricemanager.other.server.ServerModel;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;




public class OrderModel {
    //接单大厅列表
    //获取订单列表    plattype  枚举     paytype 枚举    sa_level小号等级2-11  0   task_environment 手机单还是电脑单   isbzj  1是  0否  是否有保证金
    public void getTaskViewList(final int plattype , final int paytype, final int sa_level, final int task_environment, final int isbzj, final int start, Action1 sub){
        Observable.create(new Observable.OnSubscribe<TaskEntity>() {
            @Override
            public void call(Subscriber<? super TaskEntity> subscriber) {

                    TaskEntity aaa = ServerModel.getInterface().getTaskViewList(plattype, paytype, sa_level, task_environment, isbzj, start, Config.PAGESIZE);
                subscriber.onNext(aaa);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);

    }
    //抢单  传小号实体  从同步小号后走
    public void competOrders(final int  task_Id, final SmallAccountEntity smallAccountEntity, Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().competOrders(task_Id, smallAccountEntity);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

    //抢单  第一次抢单失败  但是下次不同步
    public void competOrders(final int  task_Id, final int sa_id, Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().competOrders(task_Id, sa_id);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }




    //商家主动取消订单
    public void auditcancelOrderSJ(final int  orderid){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().auditcancelOrderSJ(orderid);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
    }




    //我的小号列表
    public void getMyAccountList(final int  plat_type,Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().getMyAccountList(plat_type);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

//取消订单
    public void cancelOrderSS(final int order_id,Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().cancelOrderSS(order_id);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }
//商家未返回  取消订单
    public void autoAuditcancelOrderSS(final int order_id){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().autoAuditcancelOrderSS(order_id);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
    }


//做单完成
    public void overOrderSS(final int order_id,final String orderCode,Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().overOrderSS(order_id,orderCode);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

    //收菜完成
    public void allOverOrderSS(final int order_id,final String orderCode,Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().allOverOrderSS(order_id,orderCode);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

    //抢单  系统60秒未响应就调这个接口
    public void autoCheckCompetOrder(final int order_id){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().autoCheckCompetOrder(order_id);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }
//    //举报
    public void complaint(final int user_id, final String content, final int type, final List<AttachmentEntity> attlist, Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().complaint(user_id,content,type,attlist);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

//    //申请仲裁
    public void arbitrateApply(final int order_id, final int user_id, final int arbi_type, final String content, final int type, final List<AttachmentEntity> attlist, Action1 sub){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().arbitrateApply(order_id,user_id,arbi_type,content,type,attlist);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

       //获取申请仲裁数据
    public void getOrderUserInfo(final int order_id, Action1 sub){
        Observable.create(new Observable.OnSubscribe<OrderEntity>() {
            @Override
            public void call(Subscriber<? super OrderEntity> subscriber) {
                OrderEntity result = ServerModel.getInterface().getOrderUserInfo(order_id);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(sub);
    }

    //获取商家审核做单完成
    public void auditOverOrderSS(final int order_id, final int allow){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().auditOverOrderSS(order_id,allow);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
    }

    //商家审核收菜完成
    public void auditAllOverOrderSS(final int order_id, final int allow){
        Observable.create(new Observable.OnSubscribe<IAcross>() {
            @Override
            public void call(Subscriber<? super IAcross> subscriber) {
                IAcross result = ServerModel.getInterface().auditAllOverOrderSS(order_id,allow);
                subscriber.onNext(result);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
    }



     OrderBroad broad;
    TextView content,wait_commit,wait_cancel;
     CountDownTimer timer;
     RxDialogSureCancel rxDialogSureCancel;
    Context context;
    View.OnClickListener click;

//    /**
//     * 弹出60秒框  等待商家响应
//     * @param context
//     * @param orderId
//     * @param type   0 代表抢单   1代表取消订单
//     * @param click
//     */
//    public void waitCommit(final Context context, final int orderId, final int type, final View.OnClickListener click){
//        this.context=context;
//        this.click=click;
//          broad=new OrderBroad();
//        //等待服务器推过来
//        context.registerReceiver(broad,new IntentFilter("qiang"));
//         rxDialogSureCancel = new RxDialogSureCancel(context);
//        View view=View.inflate(context, R.layout.wait_mechant,null);
//        rxDialogSureCancel.setContentView(view);
//          content=(TextView)view.findViewById(R.id.wait_content);
//        wait_commit=(TextView)view.findViewById(R.id.wait_commit);
//        wait_cancel=(TextView)view.findViewById(R.id.wait_cancel);
//         timer = new CountDownTimer(90000, 1000) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//                content.setText("正在呼叫商家"+(millisUntilFinished / 1000)+"s");
//                if((millisUntilFinished / 1000)>=30){
//                    wait_cancel.setEnabled(false);
//                }else{
//                    wait_cancel.setEnabled(true);
//                }
//            }
//            @Override
//            public void onFinish() {
//                wait_cancel.setVisibility(View.GONE);
//                context.unregisterReceiver(broad);
//                switch (type) {
//                    case 0:
//                        //抢单超过60秒
//                        autoCheckCompetOrder(orderId);
//                        break;
//                    case 1:
//                        //商家未返回 取消订单
//                        autoAuditcancelOrderSS(orderId);
//                        break;
//                }
//              endQiangOrder("商家未应答",click);
//            }
//        };
//        //取消
//        if(type==1){
//            wait_cancel.setVisibility(View.GONE);
//        }
//        wait_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                timer.cancel();
//                cancelOrderSS(orderId, new Action1<IAcross>() {
//                            @Override
//                            public void call(IAcross o) {
//                                if(o.m_code==200){
//
//                                }else{
//                                    Toast.makeText(context,o.m_info,Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        });
//                dialogDismiss();
//            }
//        });
//
//        rxDialogSureCancel.setCancelable(false);
//       timer.start();
//        rxDialogSureCancel.show();
//    }
    public String dialogDismiss(){
        rxDialogSureCancel.dismiss();
        return content.getText().toString();
    }

private int person;

    public int getPerson() {
        return person;
    }
    //用于的订单的抢单和取消订单
class OrderBroad extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

//       int type=intent.getIntExtra("type",0);
        //回调返回有俩种 1.抢单回调 2.取消订单回调
       String msg=intent.getStringExtra("msg");
        if(intent.getStringExtra("person")!=null){
            person=Integer.parseInt(intent.getStringExtra("person"));
        }
        wait_cancel.setVisibility(View.GONE);
//        switch(type){
//            case 0:
                //抢单
                endQiangOrder("商家回应您: "+msg,click);
//                break;
//            case 1:
//                endQiangOrder("商家回应您: "+msg,click);
                //取消订单
//                break;
//        }
        context.unregisterReceiver(broad);
    }
}
    private void endQiangOrder(String msg, View.OnClickListener listener) {
        timer.cancel();
        content.setText(msg);
        wait_commit.setVisibility(View.VISIBLE);
        wait_commit.setOnClickListener(listener);
    }



}
